import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import javax.swing.JFileChooser;
public class GUICertificateGenerator extends javax.swing.JFrame {
    
    public static GUICertificateGenerator frame;
    
    /** Creates new form GUICertificateGenerator */
    public GUICertificateGenerator() {
        initComponents();
        this.setResizable(false);
        this.setLocationRelativeTo(null);
        GUICertificateGenerator.frame = this;
        fillKeyList();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jLabel1 = new javax.swing.JLabel();
        txtName = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtCompany = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        cmbPrivateKey = new javax.swing.JComboBox();
        btnExport = new javax.swing.JToggleButton();
        btnCancel = new javax.swing.JToggleButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Locksmith - Certificate Generator");
        jLabel1.setText("Name: ");

        txtName.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtNameKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtNameKeyReleased(evt);
            }
        });

        jLabel2.setText("Company: ");

        txtCompany.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtCompanyKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtCompanyKeyReleased(evt);
            }
        });

        jLabel4.setText("Private Key:");

        cmbPrivateKey.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        btnExport.setText("Export");
        btnExport.setEnabled(false);
        btnExport.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                btnExportMousePressed(evt);
            }
        });

        btnCancel.setText("Cancel");
        btnCancel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                btnCancelMousePressed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(btnExport)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 77, Short.MAX_VALUE)
                        .add(btnCancel))
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel4)
                            .add(jLabel2)
                            .add(jLabel1))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, txtCompany, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 144, Short.MAX_VALUE)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, txtName, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 144, Short.MAX_VALUE)
                            .add(cmbPrivateKey, 0, 144, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(txtName, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel2)
                    .add(txtCompany, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(cmbPrivateKey, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel4))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(btnCancel)
                    .add(btnExport))
                .addContainerGap())
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    private void txtNameKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtNameKeyReleased
        if(txtName.getText().length() > 0)
            btnExport.setEnabled(true);
        else
            btnExport.setEnabled(false);
    }//GEN-LAST:event_txtNameKeyReleased
    
    private void txtCompanyKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtCompanyKeyReleased
        if(txtCompany.getText().length() > 0)
            btnExport.setEnabled(true);
        else
            btnExport.setEnabled(false);
    }//GEN-LAST:event_txtCompanyKeyReleased
    
    private void txtCompanyKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtCompanyKeyPressed
        if(txtCompany.getText().length() > 0)
            btnExport.setEnabled(true);
        else
            btnExport.setEnabled(false);
    }//GEN-LAST:event_txtCompanyKeyPressed
    
    private void txtNameKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtNameKeyPressed
        if(txtName.getText().length() > 0)
            btnExport.setEnabled(true);
        else
            btnExport.setEnabled(false);
    }//GEN-LAST:event_txtNameKeyPressed
    
    private void btnExportMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnExportMousePressed
        // Configure file requester
        JFileChooser fc = new JFileChooser();
        String path_source = new String("");
        String path_destination = new String("");
        fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
        fc.setFileFilter(new FilterCertificate());
        
        // Request destination file
        if (fc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
            
            File file = fc.getSelectedFile();
            path_destination = file.getAbsolutePath();
            String ext = Utilities.getExtension(file);
            if(ext == null)
                ext = "";
            
            if(!ext.equals("cer"))
                path_destination += ".cer";
            
            // Save the key to the destination file
            try {
                FileOutputStream f_out = new FileOutputStream(path_destination);
                ObjectOutputStream obj_out = new ObjectOutputStream(f_out);
                
                // Clear out the undesired parts of the key
                Key public_key = (Key) Utilities.clone(Settings.key);
                public_key.private_key = null;
                obj_out.writeObject(Boolean.valueOf(true));
                obj_out.writeObject(txtName.getText());
                obj_out.writeObject(txtCompany.getText());
                obj_out.writeObject(public_key);
                f_out.close();
                int index1 = Settings.getIndexByName(Settings.symmetric_keys,cmbPrivateKey.getSelectedItem().toString());
                int index2 = Settings.getIndexByName(Settings.assymetric_keys,cmbPrivateKey.getSelectedItem().toString());
                Key temp = Settings.key;
                Settings.key = (index1 != -1)? Settings.symmetric_keys.get(index1) : Settings.assymetric_keys.get(index2);
                boolean tempbool = Settings.public_key_selected;
                Settings.public_key_selected = false;
                new Encrypter().encryptFile(path_destination,path_destination);
                Settings.public_key_selected = tempbool;
                Settings.key = temp;
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        
        new GUIKeyManager().setVisible(true);
        this.dispose();
    }//GEN-LAST:event_btnExportMousePressed
    
    /**
     * Updates the list of keys in the interface
     */
    public void fillKeyList() {
        this.cmbPrivateKey.removeAllItems();
        
        for(int x = 0; x < Settings.symmetric_keys.size(); x++)
            this.cmbPrivateKey.addItem(Settings.symmetric_keys.get(x).name);
        for(int y = 0; y < Settings.assymetric_keys.size(); y++)
            this.cmbPrivateKey.addItem(Settings.assymetric_keys.get(y).name);
    }
    
    /**
     * Event invoked when the cancel button is pressed
     */
    private void btnCancelMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnCancelMousePressed
        new GUIKeyManager().setVisible(true);
        this.dispose();
    }//GEN-LAST:event_btnCancelMousePressed
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JToggleButton btnCancel;
    private javax.swing.JToggleButton btnExport;
    private javax.swing.JComboBox cmbPrivateKey;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JTextField txtCompany;
    private javax.swing.JTextField txtName;
    // End of variables declaration//GEN-END:variables
    
}
